# -*- encoding=utf8 -*-
import pymysql

class DBClass():

    def __init__(self):
        self.conn = pymysql.connect(host="mysql.sqlpub.com", port=3306,
                                    user="whzlearning", password="2c8bcb9077d49390", database="whzlearning", charset="utf8")

        self.cursor = self.conn.cursor()

    def closeCoCu(self):
        self.conn.close()
        self.cursor.close()

    def selectFetchall(self, sql):

        self.cursor.execute(sql)

        result = self.cursor.fetchall()

        return result

    def selectFetchone(self, sql):

        self.cursor.execute(sql)

        result = self.cursor.fetchone()

        return result


    def selectFetchmany(self, sql,num):
        self.cursor.execute(sql)

        result = self.cursor.fetchmany(num)

        return result

    def crud(self, sql):

        try:
            self.cursor.execute(sql)

            print("影响行数: ", self.conn.affected_rows())

            self.conn.commit()

        except Exception as e:

            print("执行失败，err: ",e)

            self.conn.rollback()



# if __name__ == '__main__':
#
#     DB = DBClass()
#     result1 = DB.selectFetchall("select * from student")
#
#     result2 = DB.selectFetchone("select * from student")
#
#     result3 = DB.selectFetchmany("select * from student",2)
#     DB.closeCoCu()
#
#     print(result1)
#     print(result2)
#     print(result3)